//
//  THBaseViewController.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//
// // [self.navigationController.navigationBar setTitleTextAttributes:@{            NSForegroundColorAttributeName:self.viewModel.navigationBarTextColor,
       //          NSFontAttributeName:self.viewModel.navigationBarTextFont}];
//
//
//

#import "THBaseViewController.h"
#import "THLoadingAnimateView.h"
#import "THIndicatorAnimateView.h"
@interface THBaseViewController ()
/// viewModel
@property (nonatomic,retain,readwrite) THViewModel *viewModel;
/// emptyView
@property (nonatomic,retain,readwrite) THEmptyView *emptyView;

@end

@implementation THBaseViewController

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    THBaseViewController *vc = [super allocWithZone:zone];
    @weakify(vc);
    [[vc rac_signalForSelector:@selector(viewDidLoad)]
     subscribeNext:^(RACTuple * _Nullable x) {
       @strongify(vc);
        [vc  configContainer];
        [vc  addNavbarView];
        [vc  setupUI];
        [vc  configViews];
        [vc  setupFrames];
        [vc  afterLoad];
    }];
    return vc;
}
- (instancetype)initWithViewModel:(THViewModel*)viewModel{
    if (self = [super init]) {
        self.viewModel = viewModel;
    }
    return self;
}

/// 页面唯一标识
//- (NSString *)singleFlag
//{
//    return NSStringFromClass(self.class);
//}
#pragma mark life circle
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (!self.viewModel.ignoreNarSet) {
        [self.navigationController setNavigationBarHidden:self.viewModel.hidenNavigationBar animated:animated];
        [self.navigationController.navigationBar setBarTintColor:self.viewModel.navigationBarColor];
    }
    
    [THAppEnvManager shareManager].currentController = self;
    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self layoutEmptyView];
}
#pragma mark YUEmptyView
- (THEmptyView *)emptyView {
    if (!_emptyView && self.isViewLoaded) {
        _emptyView = [[THEmptyView alloc] initWithFrame:self.view.bounds];
        [_emptyView setBackgroundColor:ColorFromHex(0xF5F7F9)];
    }
    return _emptyView;
}
- (void)showEmptyView{
    [self.view addSubview:self.emptyView];
}
- (void)showEmptyViewWithType:(THEmptyViewShowType)type
{
    //需要判断
    [self.view addSubview:self.emptyView];
    if (type == THEmptyViewShowTypeNoData) {
        [self.emptyView.lodingView removeFromSuperview];
        [self.emptyView showEmptyImageName:@"baseImg_userData_empty_icon"];
        [self.emptyView showEmptyText:@"暂无数据"];
        self.emptyView.relaodButton.hidden = YES;
    }else if (type == THEmptyViewShowTypeNoNetwork){
        [self.emptyView showEmptyImageName:@"baseImg_network_error_icon"];
        [self.emptyView showEmptyText:@"网络连接错误\n请检查网络设置或尝试重新加载"];
        [self.emptyView showBtnWithSEL:@selector(noNetworkToReload) target:self btnTitle:@"重新加载"];
        self.emptyView.relaodButton.hidden = NO;
    }else if (type == THEmptyViewShowTypeLoading){
        THLoadingAnimateView *loadingAnimateView = [[THLoadingAnimateView alloc]init];
        [self.emptyView showLodingView:loadingAnimateView];
    }else if (type == THEmptyViewShowTypeIndicatorLoading){
        THIndicatorAnimateView *indicatorAnimateView = [[THIndicatorAnimateView alloc] init];
        [self.emptyView showLodingView:indicatorAnimateView];
    }else{
        NSAssert(NO, @"空数据异常枚举,请开发自查");
    }
}
- (void)noNetworkToReload
{
    
}
- (void)hideEmptyView{
    [_emptyView removeFromSuperview];
}
- (BOOL)layoutEmptyView {
    if (_emptyView) {
        // 由于为self.emptyView设置frame时会调用到self.view，为了避免导致viewDidLoad提前触发，这里需要判断一下self.view是否已经被初始化
        //懒加载的提前加载会导致ViewController生命周期的错乱
        BOOL viewDidLoad = self.emptyView.superview && [self isViewLoaded];
        if (viewDidLoad) {
            CGSize newEmptyViewSize = self.emptyView.superview.bounds.size;
            CGSize oldEmptyViewSize = self.emptyView.frame.size;
            if (!CGSizeEqualToSize(newEmptyViewSize, oldEmptyViewSize)) {
                self.emptyView.frame = CGRectMake(CGRectGetMinX(self.emptyView.frame), CGRectGetMinY(self.emptyView.frame), newEmptyViewSize.width, newEmptyViewSize.height);
            }
            return YES;
        }
    }
    return NO;
}
/**配置 */
- (void)configContainer{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.navigationController.navigationBar.translucent = NO;//透明度
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (@available(iOS 13.0, *)) {
        self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
}
/**添加导航栏设置*/
- (void)addNavbarView{
    self.title = self.viewModel.title;
}
/**子类添加控件 */
- (void)setupUI{
}
/**子类控件属性值配置 */
- (void)configViews{
    [self.view setBackgroundColor:ColorFromHex(0xF5F7F9)];
}
/**子类控件frame配置 */
- (void)setupFrames{
}
/// 数据加载
- (void)afterLoad{}
- (UIStatusBarStyle)preferredStatusBarStyle{
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    }
    return UIStatusBarStyleDefault;
}
- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationFullScreen;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
    return UIStatusBarAnimationFade;
}

@end
